<?php
/**
 * Created by PhpStorm.
 * User: xuezhileikaku
 * Email:chengyingbo88@163.com
 * Date: 2024/8/23
 * Time: 下午6:23
 */

namespace App\Container;

use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerExceptionInterface;

class DiContainer implements ContainerInterface
{
    private $entries = [];

    public function set(string $id, $value): void
    {
        $this->entries[$id] = $value;
    }

    public function get(string $id)
    {
        if (!isset($this->entries[$id])) {
            throw new \Exception("Service {$id} not found in container");
        }

        if (is_callable($this->entries[$id])) {
            return $this->entries[$id]($this);
        }

        return $this->entries[$id];
    }

    public function has(string $id): bool
    {
        return isset($this->entries[$id]);
    }

}